package com.hw.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.hw.constants.ConstantCode;
import lombok.Data;

/**
 * @ClassName: R
 * @Description: 统一数据的返回对象
 * @Author: vie0134
 * @Date: 2020-12-10 20:47
 * @Version: 1.0
 **/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class R {
    // 常量码
    private int code;
    // 错误提示
    private String msg;
    // 返回的数据
    private Object result;

    public static R setSuccess(Object data) {
        R result = new R();
        result.setCode(ConstantCode.SUCCESS);
        result.setMsg("操作成功");
        result.setResult(data);
        return result;
    }

    public static R setError(Object data) {
        R result = new R();
        result.setCode(ConstantCode.ERROR);
        result.setMsg("操作失败");
        result.setResult(data);
        return result;
    }

    public static R setError(Object data, String msg, int code) {
        R result = new R();
        result.setCode(code);
        result.setMsg(msg);
        result.setResult(data);
        return result;
    }
}
